var app = getApp()
var {keepTwoDecimalFull} = require('../../utils/util')
var entry

Page({
  data: {
    needShow: false
  },
  onLoad: function (options) {
    entry = ''
    if (options.entry) {
      entry = options.entry
    }
    if (options.order) {
      var order = JSON.parse(decodeURIComponent(options.order))
      var orderinfo = {}
      orderinfo.order_sn = order.id
      var goodsname = []
      for (var i = 0; i < order.orderformOrderBillDOs[0].orderformOrderDetailDOs.length; i++) {
        goodsname.push(order.orderformOrderBillDOs[0].orderformOrderDetailDOs[i].NAME + ' × ' + order.orderformOrderBillDOs[0].orderformOrderDetailDOs[i].COUNT)
      }
      var goodsnamestr = goodsname.join(',')
      orderinfo.goodsname = goodsnamestr
      orderinfo.order_amount = order.payInfo ? keepTwoDecimalFull(order.payInfo.topay) : 0
      this.setData({
        order: orderinfo
      })

      this.payOrderAction()
    }
    else if (options.cake) {
      var cake = JSON.parse(decodeURIComponent(options.cake))
      var payinfo = JSON.parse(decodeURIComponent(options.payinfo))
      var goodsname = []
      for (var i = 0; i < cake.length; i++) {
        if (cake[i].COUNT) {
          goodsname.push(cake[i].name + ' × ' + cake[i].COUNT)
        }
        else {
          goodsname.push(cake[i].name + ' × ' + cake[i].num)
        }
      }
      var goodsnamestr = goodsname.join(',')
      var orderinfo = {
        goodsname: goodsnamestr,
        order_amount: keepTwoDecimalFull(payinfo.topay),
        order_sn: payinfo.lotNo
      }
      this.setData({
        order: orderinfo
      })

      this.payOrderAction()
    }
    else {
      app.toast.error('无效订单')
    }
  },

  payOrderAction: function () {
    var url = '/weappmall/user/order/create'
    var trade_no = 'ZF' + this.data.order.order_sn
    var openid = app.globalData.openID
    var sessionid = app.globalData.sessionID
    var totalfee = parseInt(parseFloat(this.data.order.order_amount) * 100)
    var params = {
      uname: " weappmall ",//固定值
      out_trade_no: trade_no,//订单号+ZF
      body: "商品购买",//固定内容
      spbill_create_ip: "127.0.0.1",
      trade_type: "JSAPI",
      openId: openid,
      attach: sessionid,
      total_fee: totalfee
    }
    app.request(url, params).then(this.payOrderCallBack.bind(this))
  },
  payOrderCallBack: function (res) {
    if (res.code !== '0000') {
      app.toast.error(res.message)
      return
    }
    app.showLoading()
    var jsapi = res.data
    var packagestr = "prepay_id=" + jsapi.prepay_id
    wx.requestPayment({
      'appId': 'wx93176ec2a9cee7e1',
      'timeStamp': jsapi.timeStamp,
      'nonceStr': jsapi.nonceStr,
      'package': packagestr,
      'signType': 'MD5',
      'paySign': jsapi.paySign,
      'success': function (res) {
        app.hideLoading()
        app.toast.success('支付成功')
        setTimeout(() => {
          wx.redirectTo({
            url: '../my/order/order?ordertype=3'
          })
        }, 1000)
      },
      'fail': function (res) {
        app.hideLoading()
        wx.redirectTo({
          url: '../my/order/order?ordertype=2',
        })
      }
    })
  },
  waitPayOrderAction: function () {
    if (entry === 'neworder') {
      wx.redirectTo({
        url: '../my/order/order?ordertype=2',
      })
    }
    else {
      wx.navigateBack()
    }
  }
})
